import axios from "axios";
import config from "../config/index.js";
import router from "../router/index.js";
import store from "../store";

import { ElMessage } from "element-plus";

const request = axios.create({
  baseURL: "https://shalong.taiyanyue.top",
  timeout: 30000,
});

request.getApi = (url) => {
  return new Promise((resolve, reject) => {
    request.get(`${url}`).then((res) => {
       if (res && res.data) {
        resolve(res);
      } else {
        console.error("接口报错", res);
        reject(res);
      }
    });
  });
};
request.postApi = ({ url, data }) => {
  return new Promise((resolve, reject) => {
    request.post(`${url}`, data).then((res) => {
      if (res && res.data) {
        resolve(res);
      } else {
        console.error("接口报错", res);
        reject(res);
      }
    });
  });
};

// request 拦截器
// 可以自请求发送前对请求做一些处理
// 比如统一加token，对请求参数统一加密
request.interceptors.request.use(
  (config) => {
    if (config.url.indexOf("http") != -1) {
      config.baseURL = "";
    }
    config.headers["Content-Type"] = "application/json;charset=utf-8";
    config.headers["token"] = localStorage.getItem("token"); // 设置请求头    return config
    config.headers["type"] = "1"; // 设置请求头    return config
    return config;
    // config.headers['token'] = user.token;  // 设置请求头    return config
  },
  (error) => {
    return Promise.reject(error);
  }
);

// response 拦截器
// 可以在接口响应后统一处理结果
request.interceptors.response.use(
  (response) => {
    if (response && response?.data) {
      if (response.data.code != 200) {
        switch (response.data.code) {
          case 501:
            store.commit("removeToken");
            store.commit("removeUserInfo");
            store.commit("$navTo", "/login");
            ElMessage.warning("登录已过期或异地登陆");
            return { data: { code: 501 } };
            break;
          default:
            ElMessage.error(response.data.message);
            return { data: { code: 1 } };
            break;
        }
      } else {
        return response;
      }
    } else {
      ElMessage.error(response.message);
      return { data: { code: 1 } };
    }
  },
  (error) => {
    /***** 接收到异常响应的处理开始 *****/
    if (error.code == "ERR_NETWORK") {
      ElMessage.error("服务器响应超时，请刷新当前页");
      return;
    }
    if (error && error.response) {
      // 1.公共错误处理
      // 2.根据响应码具体处理
      switch (error.response.status) {
        case 400:
          error.message = "错误请求";
          break;
        case 401:
          error.message = "未授权，请重新登录";
          break;
        case 403:
          error.message = "拒绝访问";
          break;
        case 404:
          error.message = "请求错误,未找到该资源";
          break;
        case 405:
          error.message = "请求方法未允许";
          break;
        case 408:
          error.message = "请求超时";
          break;
        case 500:
          error.message = "服务器端出错";
          break;
        case 501:
          error.message = "网络未实现";
          break;
        case 502:
          error.message = "网络错误";
          break;
        case 503:
          error.message = "服务不可用";
          break;
        case 504:
          error.message = "网络超时";
          break;
        case 505:
          error.message = "http版本不支持该请求";
          break;
        default:
          error.message = `连接错误${error.response.status}`;
      }
    } else {
      // 超时处理
      if (JSON.stringify(error).includes("timeout")) {
        ElMessage.error("服务器响应超时，请刷新当前页");
        return;
      } else {
        error.message = "连接服务器失败";
      }
    }
    if (error.message) {
      ElMessage.error(error.message);
      return;
    }
    /***** 处理结束 *****/
    //如果不需要错误处理，以上的处理过程都可省略
    return Promise.resolve(error.response);
  }
);

export default request;
